﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Company.StudyClub.BLL.Site;
using System.Text;
using Company.StudyClub.Model;

public partial class _Default : PageBase
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindMenu();
        }
    }

    private void BindMenu()
    {
        FunctionBLL objFunction = new FunctionBLL();
        List<Site_Functions> functionList = objFunction.GetFunctionsByUserId(CurrentUser.UserID);
        //this.dv_Menu.InnerHtml += GenerateMenuHTML(functionList);
    }

    private string GenerateMenuHTML(List<Site_Functions> functionList)
    {
        StringBuilder sb = new StringBuilder();      

        FunctionBLL objFB = new FunctionBLL();
        Site_Functions objCurrentFunction = objFB.GetModel(CurrentEnvironment.FunctionID);
        
        int currentSectionID = 0;
        int currentGroupID = 0;
        int currentFunctionID = 0;

        for (int i = 0; i < functionList.Count; i++)
        {
            Site_Functions objFunction = (Site_Functions)functionList[i];
            bool addedGroupEnd = false;

            if (objFunction.SectionID != currentSectionID)
            {
                if (currentSectionID != 0)
                {
                    sb.Append("</dl></div></div>");
                    addedGroupEnd = true;
                }

                sb.Append("<div class=\"SectionTitle\"><img src=\"");
                sb.Append(ThemePath("sidebar_icon02.png"));
                sb.Append("\" align=\"absmiddle\" alt=\"\"/> &nbsp;");
                sb.Append(objFunction.SectionName);
                sb.Append("</div>");

                currentSectionID = objFunction.SectionID;
            }

            if (objFunction.GroupID != currentGroupID)
            {
                if (currentGroupID != 0 && !addedGroupEnd)
                {
                    sb.Append("</dl></div></div>");
                }
                sb.Append("<div class=\"GroupTitle\"><div style=\"cursor:pointer;\"><img src=\"");               
                sb.Append(ThemePath("sidebar_arrow01.gif"));
                sb.Append("\" align=\"absmiddle\" alt=\"\" />&nbsp;&nbsp;");
                sb.Append(objFunction.GroupName);
                sb.Append("</div>");
                sb.Append("<div class=\"GroupContent\" ><dl>");

                currentGroupID = objFunction.GroupID;
            }

            if (objFunction.FunctionID != currentFunctionID)
            {
                sb.Append("<dd><img src=\"");
                sb.Append(ThemePath("listbar_icon02_dotted.gif"));               
                sb.Append("\" align=\"absmiddle\" alt=\"\"/><a href=\"");
                sb.Append(FilePath(objFunction.EnterUrl));
                sb.Append("\" target=\"main\">");
                sb.Append(objFunction.FunctionName);
                sb.Append("</a>");
                sb.Append("</dd>");

                currentFunctionID = objFunction.FunctionID;
            }

            if (i == functionList.Count - 1)
            {
                sb.Append("</dl></div></div>");
            }
        }

        return sb.ToString();
    }
}
